// import { lazy } from 'react'
import $copy from '@utils/copy'
/* -------------------------------------------------------- Declare -- */
const { $lazy } = window
/* -------------------------------------------------------- Async Component -- */
const SearchTable = $lazy.hook(()=>import('#cpt/temp/SearchTable'))
const typeData =[
	'table', 'sea_orm', 'sea_orm_file', 'sea_orm_audit',
	'react', 'react_file', 'migration',
]
/* -------------------------------------------------------- Page Component -- */
export const Component = () => {
	// 编辑添加配置
	const addConfig = {
		modal: {
			width 		: '99%',
			height 		: '99%'
		},
		form: {
			labelWidth 	: 's5',
			param: { level:0 },
			data: [
				{ label: '模板名称',		name: 'name', 			r:1, maxLen:30 },
				{ label: '英文名称',		name: 'name_en', 		r:1, maxLen:30 },
				{ label: '模板类型', 	name: 'type', 			r:1, type:'select', data:typeData },
				{ label: '排序',		name: 'level', 			r:1, type:'number' },
				{ label: '模板内容', 	name: 'content', 		r:1, type:'textarea', maxLength:10000, rows:15 },
				{ label: '联合模板', 	name: 'content_union', 	type:'textarea', maxLength:10000, rows:15 },
				{ label: '前端模板', 	name: 'content_front', 	type:'textarea', maxLength:10000, rows:15 },
			],
			
		}
	}
	const onInit = () => {
		const form = document.querySelector('.note-form')
		setTimeout(()=>{
			form.querySelector('textarea').onkeydown = function(e){
				if (e.keyCode === 9){
					e.preventDefault()
					var indent = '	'
					var start = this.selectionStart
					var end = this.selectionEnd
					var selected = window.getSelection().toString()
					selected = indent + selected.replace(/\n/g, '\n' + indent)
					this.value = this.value.substring(0, start) + selected + this.value.substring(end)
					this.setSelectionRange(start + indent.length, start + selected.length)
				}
			}
		},200)
	}
	const config = {
		page : {
			// title: '新闻管理',
			controls	: [
				{ label:'主模板', mode:'formModal', name:'content', isKeys:1, r:1, config: ({ label, rows }) => ({
					modal: {
						title 		: label,
						width 		: '95%',
						height 		: '95%',
						controls 	: {
							after: [{ label:'复制', ghost:1, click: () => $copy( rows().content ) }]
						}
					},
					form: {
						className 	: 'note-form',
						api 		: 'template',
						way 		: 'put',
						param 		: { ...rows() },
						data: [
							{ name: 'content', type:'textarea', maxLength:null },
						],
						onInit
					}
				}) },
				{ label:'次模板', mode:'formModal', name:'content_union', isKeys:1, r:1, config: ({ label, rows }) => ({
					modal: {
						title 		: label,
						width 		: '95%',
						height 		: '95%',
						controls 	: {
							after: [{ label:'复制', ghost:1, click: () => $copy( rows().content_union ) }],
						}
					},
					form: {
						className 	: 'note-form',
						api 		: 'template',
						way 		: 'put',
						param 		: { ...rows() },
						data: [
							{ name: 'content_union', type:'textarea', maxLength:null },
						],
						onInit
					}
				}) },
				{ label:'新增', mode:'add', r:1 },
				{ mode:'refresh'}
			]
		},
		search : {
			data:[
				{ label: '模板名称', 	name:'name' },
				{ label: '模板类型', 	name: 'type', type:'select', data:typeData },
			]
		},
		table: {
			api 		: 'template',
			addConfig,
			clearCache  : 'template/dict',
			row 		: { type:1, is_rows:1 },
			cols		: [
				{ title: '模板名称', 			dataIndex: 'name', 		width:100, sorter:1 },
				{ title: '英文名称', 			dataIndex: 'name_en', 	width:100, sorter:1 },
				{ title: '排序', 				dataIndex: 'level', 	width:60 },
				{ title: '模板类型', 			dataIndex: 'type', 		width:100 },
			],
			controls 	: {
				width: 200,
				data: v => [
					{ mode:'mod', config:addConfig, rs:0 },
					{ mode:'delPop' },
					{ label:'主模板', mode:'formModal', r:1, config: () => ({
						modal: {
							title 		: v.name,
							width 		: '95%',
							height 		: '95%',
							controls 	: {
								after: [{ label:'复制', ghost:1, click: () => $copy( v.content ) }]
							}
						},
						form: {
							className 	: 'note-form',
							api 		: 'template',
							way 		: 'put',
							param 		: v,
							data: [
								{ name: 'content', type:'textarea', maxLength:null },
							],
							onInit
						}
					}) },
					{ label:'次模板', mode:'formModal', r:1, disabled: !v.content_union, config: () => ({
						modal: {
							title 		: v.name,
							width 		: '95%',
							height 		: '95%',
							controls 	: {
								after: [{ label:'复制', ghost:1, click: () => $copy( v.v.content_union ) }]
							}
						},
						form: {
							className 	: 'note-form',
							api 		: 'template',
							way 		: 'put',
							param 		: v,
							data: [
								{ name: 'content_union', type:'textarea', maxLength:null },
							],
							onInit
						}
					}) },
					{ label:'复制模板', click: () => $copy( v.content ) },
				]
			}
		}
	}
	return <SearchTable {...config}/>
}
export default Component